/**
 * File: UtilizadorController.java
 * Data: 11.11.2010
 * Objective: Criar a classe controller da entidade Utilizador.
 *
 * Copyright 2010 S2DZ CORPORATION
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package controller;

import java.sql.Date;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import modelo.UtilizadorModelo;

/**
 * Controller da entidade Log.
 * @author Paulo Ilenga
 */
public class UtilizadorController extends ControllerGeneric
{
    private UtilizadorModelo modelo;

    @Override
    public boolean save(Object object)
    {
        modelo = (UtilizadorModelo) object;

        connect();

        sql = "INSERT INTO tbl_utilizador(id_funcionario, username, password, privilegios) VALUES (?, ?, ?)";

        try
        {
            ps = connection.prepareStatement(sql);

            ps.setInt(1, modelo.getIdFuncionario());
            ps.setString(2, modelo.getUsername());
            ps.setString(3, modelo.getPassword());
            ps.setString(4, modelo.getPrivilegios());

            result = ps.execute();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(UtilizadorController.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            disconnect();
        }

        return result;
    }

    @Override
    public boolean update(Object object)
    {
        modelo = (UtilizadorModelo) object;

        connect();

        sql = "UPDATE tbl_utilizador SET id_funcionario=?, username=?, password=?, privilegios=? where id=?";

        try
        {
            ps = connection.prepareStatement(sql);

            ps.setInt(1, modelo.getIdFuncionario());
            ps.setString(2, modelo.getUsername());
            ps.setString(3, modelo.getPassword());
            ps.setString(4, modelo.getPrivilegios());

            ps.setInt(5, modelo.getId());

            result = ps.execute();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(UtilizadorController.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            disconnect();
        }

        return result;
    }

}
